class Solution(object):
    def minSubArrayLen(self, target, nums):
        """
        :type target: int
        :type nums: List[int]
        :rtype: int
        """
        # min_len = float('inf')
        # for i in range(len(nums)):
        #     sum = 0
        #     for j in range(i,len(nums)):
        #         sum += nums[j]
        #         if sum >= target:
        #             min_len = min(min_len,j - i + 1)
        #             break
        # return min_len if min_len != float('inf') else 0

        # 滑动窗口法
        l = len(nums)
        left = 0
        right = 0
        min_len = l + 1
        sum = 0

        for right in range(l):
            sum += nums[right]

            while sum >= target:
                min_len = min(min_len, right - left + 1)
                sum -= nums[left]
                left += 1

        return min_len if min_len != l + 1 else 0


if __name__ == '__main__':
    nums = [2, 3, 1, 2, 4, 3]
    target = 7
    print(Solution().minSubArrayLen(target, nums))
